<?php
if(isset($_POST['submit']))
{

	if(isset($_POST['dbname'])){ $dbname = $_POST['dbname']; }
	if(isset($_POST['dbuser'])){ $dbuser = $_POST['dbuser']; }
	if(isset($_POST['dbpass'])){ $dbpass = $_POST['dbpass']; }
	if(isset($_POST['username'])){ $adname = $_POST['username']; }
	if(isset($_POST['password'])){ $adpass = $_POST['password']; }


	// Try to connect to the database
	$connect = 	@mysql_connect($_POST['dbhost'], $_POST['dbuser'], $_POST['dbpass']);
	
				mysql_query('SET NAMES UTF8');
				
	$select = 	@mysql_select_db($_POST['dbname'], $connect);

	// Handle the install
	if(!$select)
	{
		$errors[] = 'I cannot connect to the database. Make sure the database exists and you entered right info.';
	}
	elseif(!is_file($_POST['root'] . '/index.php'))
	{
		$errors[] = 'Wrong root directory!';
	}
	elseif(empty($_POST['prefix']))
	{
		$errors[] = 'Please enter tables prefix.';
	}
	else
	{
		$complete_config = array(
		'PUNG_ROOT' 		=> $_POST['root'],
		'PUNG_URL'			=> $_POST['url'],
		'DB_PREFIX' 		=> $_POST['prefix'],
		'DB_USER' 			=> $_POST['dbuser'],
		'DB_PASS' 			=> $_POST['dbpass'],
		'DB_HOST' 			=> $_POST['dbhost'],
		'DB_NAME' 			=> $_POST['dbname'],
		'THEME' 			=> 'pungcms',
		'MODULE' 			=> 'none',
		'LANGUAGE' 			=> 'english',
		'PAGETITLE' 		=> 'PungCMS',
		'SITENAME' 			=> 'PungCMS',
		'ENCODING' 			=> 'utf-8',
		'FOOTER' 			=> '&copy;2010 All Rights Reserved. Powered by PungCMS v1.0,',
		'DEBUG_MODE' 		=> 'on',
		'SLOGAN' 			=> 'Everbody can make website.',
		'KEYWORDS' 			=> 'PungCMS, Thai CMS, ThaiCMS',
		'DESCRIPTION' 		=> 'Description of your website',
		'ADMIN_TIPS' 		=> 'on',
		'SESSION_ADMIN' 	=> 'gfd76g5sd76fgsd9',
		'PHPCONSOLE' 		=> 'on',
		'ADMIN_USER' 		=> $_POST['username'],
		'ADMIN_PASS' 		=> $_POST['password']
		);
		
		$fh = fopen('../config.php', 'w') or die('Unknown error with config manager.');
		
		$filedata =	'<?php' . "\n";
		fwrite($fh, $filedata);
		
		foreach($complete_config as $key => $value)
		{
			$filedata =	'define(\'' . $key . '\', \'' . $value . '\');' . "\n";
			fwrite($fh, $filedata);
		}
		
		$filedata =	'?>';
		fwrite($fh, $filedata);
	
		fclose($fh);
		
		include('../config.php');
		include('sql.php');
		
		$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or exit('Cannot connect to database.');
		
		mysql_query('SET NAMES UTF8');
		
		mysql_select_db(DB_NAME, $connect) or exit('Database does not exist.');
		
		foreach($sql as $query)
		{
			mysql_query($query);
		}
		
		header('Location: ' . PUNG_URL);
	}
}else{

	$dbname = "pungcms";
	$dbuser = "root";
	$dbpass = "";
	$adname = "admin";
	$adpass = "";

}

// Get document root
$ppp = $_SERVER['SCRIPT_FILENAME'];
$ppp = explode('/install', $ppp);
$ppp = $ppp['0'];

// Get website URL
function curPageURL() {
	$pageURL = 'http';

	if(isset($_SERVER['HTTPS'])) {
		if ($_SERVER["HTTPS"] == "on") { $pageURL .= "s"; }
	}

	$pageURL .= "://";

	if ($_SERVER["SERVER_PORT"] != "80") {
		$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
	} else {
		$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
	}
 
 	$pageURL = explode('/install', $pageURL);

	$pageURL = $pageURL['0'];
 
	return $pageURL;
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
	<title>PungCMS 1.0 Install</title>
	<meta content="text/html; charset=utf-8" http-equiv="content-type" />
	<link href="../admin/style.css" rel="stylesheet" type="text/css" />
</head>

<body>

<div id="installbox" style="margin: 20px; margin-left: 250px; width: 600px; border: 1px solid #ccc; padding: 10px">

<?php
if(!empty($errors))
{
	print '<div id="error">' . $errors['0'] . '';
}
?>

<h3>PungCMS 1.0 install</h3>

<form action="" method="post">
<table width="100%">
<tr>
	<td width="130"><strong>Database host:</strong></td>
	<td width="200"><input type="text" name="dbhost" value="localhost" style="width: 200px" /></td>
	<td style="font-size:10px">This is usually <em>localhost</em>.</td>
</tr>
<tr>
	<td><strong>Database name:</strong></td>
	<td><input type="text" name="dbname" style="width: 200px" value="<?php print $dbname; ?>" /></td>
	<td style="font-size:10px"></td>
</tr>
<tr>
	<td><strong>Database username:</strong></td>
	<td><input type="text" name="dbuser" style="width: 200px" value="<?php print $dbuser; ?>" /></td>
	<td style="font-size:10px"></td>
</tr>
<tr>
	<td><strong>Database password:</strong></td>
	<td><input type="text" name="dbpass" style="width: 200px" value="<?php print $dbpass; ?>" /></td>
	<td style="font-size:10px"></td>
</tr>
<tr>
	<td><strong>Tables prefix:</strong></td>
	<td><input type="text" name="prefix" value="pungcms_" style="width: 200px" /></td>
	<td style="font-size:10px">Prefix for table names.</td>
</tr>
<tr>
	<td><strong>Root directory:</strong></td>
	<td><input type="text" name="root" value="<?php print $ppp; ?>" style="width: 200px" /></td>
	<td style="font-size:10px">Leave default if unsure.</td>
</tr>
<tr>
	<td><strong>Website URL:</strong></td>
	<td><input type="text" name="url" style="width: 200px" value="<?php print curPageURL(); ?>" /></td>
	<td style="font-size:10px">Example: <em>http://www.domain.com</em></td>
</tr>
<tr>
	<td><strong>Admin username:</strong></td>
	<td><input type="text" name="username" style="width: 200px" value="<?php print $adname; ?>" /></td>
	<td style="font-size:10px">Control panel username.</td>
</tr>
<tr>
	<td><strong>Admin password:</strong></td>
	<td><input type="text" name="password" style="width: 200px" value="<?php print $adpass; ?>" /></td>
	<td style="font-size:10px">Control panel password.</td>
</tr>
</table>
<br />
<input type="submit" value="Install now ..." name="submit" class="button" />
</form>

</div>

</body>
</html>